<script setup lang="ts">
import { patientConsultOrderPre, patientInfo, patientConsultOrder } from '@/api/consult'
import { ConsultType, EconsultFlag } from '@/enums/index'
import type { patientConsultOrderPreApi } from '@/types/consult'
import { ref } from 'vue'
import { useConsult } from '@/stores'
import type { patientInfoApi } from '@/types/consult'
import { showFailToast } from 'vant'
import CpPay from '@/components/cp-pay.vue'
const payInfo = ref<patientConsultOrderPreApi>()
// 支付信息
const getData = async () => {
  const res = await patientConsultOrderPre({
    type: ConsultType.Fast,
    illnessType: EconsultFlag.YES
  })
  payInfo.value = res.data
  console.log('支付信息', res)
}

// 患者信息获取
const store = useConsult()
const info = ref<patientInfoApi>()
const getConsultInfo = async () => {
  const res = await patientInfo(store.consultInfo?.patientId!)
  info.value = res.data
  console.log('患者信息', res)
}

getData()
getConsultInfo()

// 支付下订单
const agree = ref(false)
const orderId = ref<string>('')
const loading = ref(false)
const pay = ref<InstanceType<typeof CpPay>>()
const submit = async () => {
  if (!agree.value) {
    return showFailToast('请先勾选协议')
  }
  // 下订单
  loading.value = true
  try {
    const res = await patientConsultOrder(store.consultInfo!)
    orderId.value = res.data.id
    // 打开支付弹窗
    pay.value!.show = true
    console.log('订单信息', res)
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="consult-pay-page" v-if="payInfo">
    <cp-nav-bar title="支付" />
    <div class="pay-info">
      <p class="tit">图文问诊 {{ payInfo?.payment }} 元</p>
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p class="desc">
        <span>极速问诊</span>
        <span>自动分配医生</span>
      </p>
    </div>
    <van-cell-group>
      <van-cell title="优惠券" :value="`-¥${payInfo?.couponDeduction}`" />
      <van-cell title="积分抵扣" :value="`-¥${payInfo?.pointDeduction}`" />
      <van-cell title="实付款" :value="`-¥${payInfo?.actualPayment}`" class="pay-price" />
    </van-cell-group>
    <div class="pay-space"></div>
    <van-cell-group>
      <van-cell
        title="患者信息"
        :value="`${info?.name} | ${info?.genderValue} | ${info?.age}岁`"
      ></van-cell>
      <van-cell title="病情描述" :label="store.consultInfo?.illnessDesc"></van-cell>
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox v-model="agree">我已同意 <span class="text">支付协议</span></van-checkbox>
    </div>
    <van-submit-bar
      button-type="primary"
      :price="payInfo?.actualPayment * 100"
      button-text="立即支付"
      text-align="left"
      :loading="loading"
      @submit="submit"
    />

    <!-- 弹窗支付 -->
    <CpPay ref="pay" :money="payInfo?.actualPayment" :orderId="orderId"></CpPay>
  </div>
</template>

<style lang="scss" scoped>
.consult-pay-page {
  padding: 46px 0 50px 0;
}
.pay-info {
  display: flex;
  padding: 15px;
  flex-wrap: wrap;
  align-items: center;
  .tit {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }
  .img {
    margin-right: 10px;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    overflow: hidden;
  }
  .desc {
    flex: 1;
    > span {
      display: block;
      color: var(--cp-tag);
      &:first-child {
        font-size: 16px;
        color: var(--cp-text2);
      }
    }
  }
}
.pay-price {
  ::v-deep() {
    .vam-cell__title {
      font-size: 16px;
    }
    .van-cell__value {
      font-size: 16px;
      color: var(--cp-price);
    }
  }
}
.pay-space {
  height: 12px;
  background-color: var(--cp-bg);
}
.pay-schema {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  .text {
    color: var(--cp-primary);
  }
}
::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}
</style>
